package com.scrcu.core.batch.biz;

import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.scrcu.core.batch.dao.SqlSessionDao;
import com.scrcu.core.batch.entity.BatchEvent;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BatchEventBizImpl extends SqlSessionDao implements BatchEventBiz {

    public BatchEventBizImpl(SqlSession dao) {
        super(dao);
    }

    private final String namespace = "com.scrcu.core.batch.BatchEvent.";

    @Override
    @Transactional

    public String save(BatchEvent evt) {
        if (Util.strIsEmpty(evt.getId())) {
            dao.insert(namespace.concat("insertSelctive"), evt);
        } else {
            dao.update(namespace.concat("updateSelective"), evt);
        }
        return evt.getId();
    }

    @Override
    @Transactional(readOnly = true)
    public PagedResult<BatchEvent> list(String exeId, String endTime, int pageNo, int pageSize) {
        Map paramMap = new HashMap<>();
        paramMap.put("exeId", exeId);
        paramMap.put("endTime", endTime);
        paramMap.put("start", pageNo * pageSize);
        paramMap.put("pageSize",pageSize);
        int total = dao.selectOne(namespace.concat("selectCount"), paramMap);
        List<BatchEvent> lt = dao.selectList(namespace.concat("selectForList"), paramMap);
        return new PagedResult<BatchEvent>(total, lt, pageNo, pageSize);
    }

    @Override
    @Transactional(readOnly = true)
    public BatchEvent get(String id) {
        return dao.selectOne(namespace.concat("selectById"), id);
    }
}